package at.com.vn.chess.util;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import at.com.vn.chess.entity.ChessRow;
import at.com.vn.chess.listener.DialogClickListener;

public class ChessTable extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Table
	String rowData[][] = { { "1", "wait", "wait", "idle" } };

	String fields[] = { "Table", "BlackOwner", "WhiteOwner", "Status" };

	JButton btn = new JButton( "Create New Chess Table" );

	DefaultTableModel model;

	JTable table = new JTable();

	JScrollPane jsp;

	ArrayList<ChessRow> lstCellEff = new ArrayList<ChessRow>();

	DialogClickListener mListener;

	public ChessTable() {
		super();
		initTable();
		lstCellEff.add( new ChessRow( 1, "", "wait", "wait", 0, 0 ) );
	}

	public void RefreshTable( String userID, ArrayList<ChessRow> lstDataRow ) {
		lstCellEff = lstDataRow;
		setDataLink( lstCellEff );
	}

	void initTable() {
		model = new DefaultTableModel( rowData, fields );
		table.setModel( model );
		setPreferredSize( new Dimension( 248, 450 ) );
		setMaximumSize( getPreferredSize() ); // prevent growth
		setMinimumSize( getPreferredSize() ); // prevent shrink
		// Disable auto resizing
		table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 40 );
		table.getColumnModel().getColumn( 1 ).setPreferredWidth( 80 );
		table.getColumnModel().getColumn( 2 ).setPreferredWidth( 80 );
		table.getColumnModel().getColumn( 3 ).setPreferredWidth( 60 );

		jsp = new JScrollPane( table );
		add( btn );
		btn.setBounds( 5, 5, 180, 30 );
		add( jsp );
		jsp.setBounds( 5, 40, 260, 585 );

		btn.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed( ActionEvent e ) {
				mListener.createRowClicked();
			}

		} );
	}

	public void setDataLink( ArrayList<ChessRow> lstEff ) {
		if ( model.getRowCount() == 0 ) {
			return;
		}
		HyperLinkColumn hyperLinkColumn = new HyperLinkColumn( table,
				new SampleAction(), lstEff );
		hyperLinkColumn.setMnemonic( KeyEvent.VK_D );
	}

	public void setOnClickListener( DialogClickListener listener ) {
		mListener = listener;
	}

	public DefaultTableModel getModel() {
		return model;
	}

	public JTable getTable() {
		return table;
	}

	private class SampleAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public SampleAction() {
			super.putValue( Action.NAME, "Sample Action" );
			super.putValue( Action.SHORT_DESCRIPTION, "Click to join to play chess" );
		}

		public void actionPerformed( ActionEvent e ) {
			// JTable table = (JTable) e.getSource();
			try {
				String cell = e.getActionCommand();
				int modelCol = Integer
						.valueOf( cell.substring( cell.indexOf( ',' ) + 1 ) );

				int modelRow = Integer.valueOf( cell.substring( 0, cell
						.indexOf( ',' ) ) );

				mListener.linkClicked( modelRow, modelCol );
			} catch ( NumberFormatException ex ) {
				return;
			}
		}
	}
}
